package cn.tedu.oa.sys.controller;

import cn.tedu.oa.common.annotation.LogAnnotation;
import cn.tedu.oa.common.util.ShiroUtils;
import cn.tedu.oa.common.vo.JsonResult;
import cn.tedu.oa.sys.entity.Menu;
import cn.tedu.oa.sys.entity.User;
import cn.tedu.oa.sys.service.MenuService;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/sys/menu/")
public class MenuController {
    @Autowired
    private MenuService menuService;

    /*菜单主页 */
    @LogAnnotation("菜单：访问菜单主页")
    @RequestMapping("menu_ui")
    public String menu_ui() {
        return "sys/menu/menu_ui";
    }

    /*菜单主页 */
    @LogAnnotation("菜单：访问树形菜单首页")
    @RequestMapping("menu_ui_tree")
    public String menu_ui_tree() {
        return "sys/menu/menu_ui_tree";
    }

    /*访问确定了上级菜单而增加菜单页面 */
    @LogAnnotation("菜单：访问根据上级菜单增加菜单页面")
    @RequestMapping("menu_addByParentId")
    public String addByParentId() {
        return "sys/menu/menu_addByParentId";
    }

    /*显示数据列表/分页查询*/
    @LogAnnotation("菜单：菜单分页查询")
    @RequestMapping("get")
    @ResponseBody
    public JsonResult get(String criteria, Integer pageNo, Integer pageSize) {
        List<Menu> list = menuService.getMenuPagerList(criteria, pageNo, pageSize);
        int rowCount = menuService.getRowCount(criteria);
        JsonResult result = new JsonResult();
        result.setCount(rowCount);
        result.setData(list);
        return result;
    }

    /*显示所有数据*/
    @LogAnnotation("菜单：获取菜单所有数据")
    @RequestMapping("getAll")
    @ResponseBody
    public JsonResult getAll() {
        List<Menu> list = menuService.getAll();
        int rowCount = menuService.getRowCount();
        JsonResult result = new JsonResult();
        result.setCount(rowCount);
        result.setData(list);
        return result;
    }

    /*菜单添加页 */
    @LogAnnotation("菜单：访问菜单添加页面")
    @RequestMapping("menu_add")
    public String menu_add() {
        return "sys/menu/menu_add";
    }

    /*菜单编辑页 */
    @LogAnnotation("菜单：访问菜单编辑页面")
    @RequestMapping("menu_edit")
    public String menu_update() {
        return "sys/menu/menu_edit";
    }


    /*编辑菜单*/
    @LogAnnotation("菜单：编辑菜单功能")
    @RequestMapping("edit")
    @ResponseBody
    public JsonResult edit(Menu menu) {
        menu.setUpdateUser(ShiroUtils.getPrincipal().getName());
        menu.setUpdateTime(new Date());
        menuService.update(menu);
        return new JsonResult("编辑成功");
    }

    /*添加菜单*/
    @LogAnnotation("菜单：添加菜单功能")
    @RequestMapping("add")
    @ResponseBody
    public JsonResult add(Menu menu) {
        menuService.add(menu);
        return new JsonResult("添加成功");
    }

    /*根据id删除信息*/
    @LogAnnotation("菜单：删除单个菜单功能")
    @RequestMapping("deleteById")
    @ResponseBody
    public JsonResult deleteById(String id) {
        menuService.delete(Integer.parseInt(id));
        return new JsonResult("delect ok");
    }

    /*根据多个id删除信息*/
    @LogAnnotation("菜单：删除多个菜单功能")
    @RequestMapping("deleteByIds")
    @ResponseBody
    public JsonResult deleteByIds(Integer... ids) {
        menuService.deleteByIds(ids);
        return new JsonResult("delect ok");
    }

    @LogAnnotation("菜单：根据菜单名获取数据")
    @ResponseBody
    @RequestMapping("getMenuByName")
    public JsonResult getMenuByName(String name){
        Menu menu = menuService.getMenuByName(name);
        JsonResult result = new JsonResult();
        result.setData(menu);
        return result;
    }

    @LogAnnotation("菜单：根据上级菜单获取下级菜单")
    @ResponseBody
    @RequestMapping("getSubMenusByPreviousMenuId")
    public JsonResult getSubMenusByPreviousMenuId(Integer id){
        List<Menu> list = menuService.getSubMenusByPreviousMenuId(id);
        JsonResult result = new JsonResult();
        result.setData(list);
        return result;
    }

    @LogAnnotation("菜单：获取顶级菜单" )
    @ResponseBody
    @RequestMapping("getFirstMenus")
    public JsonResult getFirstMenus(){
        List<Menu> list = menuService.getFirstMenus();
        JsonResult result = new JsonResult();
        result.setData(list);
        return result;
    }

    @LogAnnotation("菜单：根据用户获取菜单")
    @ResponseBody
    @RequestMapping("getMenusByUser")
    public JsonResult getMenusByUser(Integer previousMenuId){
        List<Menu> list = menuService.getMenusByUser(previousMenuId);
        JsonResult result = new JsonResult();
        result.setData(list);
        return result;
    }
}
